package com.ujs.shc.controller;

import com.google.gson.Gson;
import com.ujs.shc.dto.FocusGoodsDto;
import com.ujs.shc.pojo.Focus;
import com.ujs.shc.pojo.Goods;
import com.ujs.shc.pojo.Image;
import com.ujs.shc.pojo.User;
import com.ujs.shc.service.FocusService;
import com.ujs.shc.service.GoodsService;
import com.ujs.shc.service.ImageService;
import com.ujs.shc.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/focus")
public class FocusController {

    @Autowired
    private ImageService imageService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FocusService focusService;


    private Gson gson = new Gson();

    /**
     * 查看用户所有关注（登录状态）
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allFocus",method = {RequestMethod.GET, RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String allFocus(HttpServletRequest request){

        Msg msg = new Msg(0);
        User currentUser = (User)request.getSession().getAttribute("currentUser");
        if(currentUser == null){
            msg.setMessage("未登录");
            return gson.toJson(msg);
        }
        List<Goods> focusGoodsList = goodsService.getFocusGoodsesByUid(currentUser.getId());

        List<FocusGoodsDto> focusGoodsDtoList = new ArrayList<>();
        for(Goods good:focusGoodsList){


            System.out.println(good);
            FocusGoodsDto dto = new FocusGoodsDto();
            dto.setId(good.getId());
            dto.setName(good.getName());
            dto.setContent(good.getDescrible());

            List<Image> images = imageService.getImagesByGoodsPrimaryKey(good.getId());
            if(images != null && images.size()>0){
                String imgUrl = images.get(0).getImgUrl();
                dto.setImgUrl(imgUrl);
            }
            if(dto.getEndDate() == null){
                dto.setEndDate("");
            }
            focusGoodsDtoList.add(dto);
        }
        if(focusGoodsDtoList != null){
            msg.setCode(1);
            msg.setData(focusGoodsDtoList);
        }
        return gson.toJson(msg);
    }

    /**
     * 添加关注商品（登录状态）
     * @param request
     * @param goodsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addFocus",method = {RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String addFocus(HttpServletRequest request, Integer goodsId){

        Msg msg = new Msg(0);
        User currentUser = (User)request.getSession().getAttribute("currentUser");
        if(currentUser == null){
            msg.setMessage("未登录");
            return gson.toJson(msg);
        }
        Integer userId = currentUser.getId();
        Focus focus = focusService.getFocusByGidAndUid(goodsId, userId);
        if(focus != null){
            msg.setMessage("已关注");
            return gson.toJson(msg);
        }
        Integer rst = focusService.addFocus(goodsId, userId);
        if(rst > 0){
            msg.setCode(1);
            return gson.toJson(msg);
        }

        return gson.toJson(msg);
    }

    /**
     * 取消关注商品（登录状态）
     * @param request
     * @param goodsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/removeFocus",method = {RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String removeFocus(HttpServletRequest request, Integer goodsId){

        Msg msg = new Msg(0);
        User currentUser = (User)request.getSession().getAttribute("currentUser");
        if(currentUser == null){
            msg.setMessage("未登录");
            return gson.toJson(msg);
        }
        Integer userId = currentUser.getId();
        Focus focus = focusService.getFocusByGidAndUid(goodsId, userId);
        if(focus == null){
            msg.setMessage("未关注");
            return gson.toJson(msg);
        }
        Integer rst = focusService.removeFocus(goodsId, userId);
        if(rst > 0){
            msg.setCode(1);
            return gson.toJson(msg);
        }
        return gson.toJson(msg);
    }


}
